
let temperature_outside = null;
let humidity_outside = null;

let temperature_bedroom = null
let humidity_bedroom = null
let temperature_livingroom = null
let humidity_livingroom = null
let temperature_marcieroom = null
let humidity_marcieroom = null

let outside_sid = '158d00022c944c';

let livingroom_sid = '158d000182e8ee'
let bedroom_sid = '158d0000e87847'
let marcieroom_sid = '158d000182ea4e'

let events = require('events');
let miSensorsEvent;

// const { ipcMiSensors } = require('electron')

module.exports = function(eventSend) {
	miSensorsEvent = eventSend;
};


let dgram = require('dgram');
let server = dgram.createSocket('udp4');

server.on('message', onMessage);
server.bind(9898, () => {
	server.addMembership('224.0.0.50');
})

function onMessage(msg, rinfo) {
	let o = JSON.parse(msg);
	if (o.sid == outside_sid) {
		// Outside
		if (o.data == null) {
			return;
		}

		data = JSON.parse(o.data);
		if (data.temperature != null) {
			if (temperature_outside != data.temperature) {
				temperature_outside = data.temperature;
				miSensorsEvent.send('outside_temp', temperature_outside);
			}
		}

		if (data.humidity != null) {
			if (humidity_outside != data.humidity) {
				humidity_outside = data.humidity;
				miSensorsEvent.send('outside_hum', humidity_outside);
			}
		}
	} else if (o.sid == livingroom_sid) {
		// livingroom
		if (o.data == null) {
			return;
		}

		data = JSON.parse(o.data);
		if (data.temperature != null) {
			if (temperature_livingroom != data.temperature) {
				temperature_livingroom = data.temperature;
				miSensorsEvent.send('livingroom_temp', temperature_livingroom);
			}
		}

		if (data.humidity != null) {
			if (humidity_livingroom != data.humidity) {
				humidity_livingroom = data.humidity;
				miSensorsEvent.send('livingroom_hum', humidity_livingroom);
			}
		}
	} else if (o.sid == bedroom_sid) {
        // bedroom
        if (o.data == null) {
            return
        }

        data = JSON.parse(o.data)
        if (data.temperature != null) {
            if (temperature_bedroom != data.temperature) {
                temperature_bedroom = data.temperature
                miSensorsEvent.send('bedroom_temp', temperature_bedroom)
            }
        }

        if (data.humidity != null) {
            if (humidity_bedroom != data.humidity) {
                humidity_bedroom = data.humidity
                miSensorsEvent.send('bedroom_hum', humidity_bedroom)
            }
        }
	} else if (o.sid == marcieroom_sid) {
        // marcieroom
        if (o.data == null) {
            return
        }

        data = JSON.parse(o.data)
        if (data.temperature != null) {
            if (temperature_marcieroom != data.temperature) {
                temperature_marcieroom = data.temperature
                miSensorsEvent.send('marcieroom_temp', temperature_marcieroom)
            }
        }

        if (data.humidity != null) {
            if (humidity_marcieroom != data.humidity) {
                humidity_marcieroom = data.humidity
                miSensorsEvent.send('marcieroom_hum', humidity_marcieroom)
            }
        }
    }
}

setTimeout( function() {
	let readMsg = '{"cmd":"read", "sid":"' + livingroom_sid + '"}';
	server.send(readMsg, 9898, '192.168.2.163');

	readMsg = '{"cmd":"read", "sid":"' + outside_sid + '"}';
	server.send(readMsg, 9898, '192.168.2.163');

    readMsg = '{"cmd":"read", "sid":"' + bedroom_sid + '"}';
	server.send(readMsg, 9898, '192.168.2.163');

    readMsg = '{"cmd":"read", "sid":"' + marcieroom_sid + '"}';
	server.send(readMsg, 9898, '192.168.2.163');
}, 5000);


// exports.miSensorsEvent = miSensorsEvent;

